<?php

require __DIR__ . '/vendor/autoload.php';

use app\model\Category;

try {
    // 初始化应用
    $app = new \think\App();

    // 插入系统分类
    $systemCategories = [
        [
            'user_id' => null,
            'name' => '开发工具',
            'icon' => '💻',
            'color' => '#1890ff',
            'description' => '编程开发相关的账号',
            'sort' => 1,
            'is_system' => 1,
            'is_active' => 1,
        ],
        [
            'user_id' => null,
            'name' => '社交媒体',
            'icon' => '📱',
            'color' => '#52c41a',
            'description' => '社交平台和媒体账号',
            'sort' => 2,
            'is_system' => 1,
            'is_active' => 1,
        ],
        [
            'user_id' => null,
            'name' => '云服务',
            'icon' => '☁️',
            'color' => '#722ed1',
            'description' => '云计算和网络服务',
            'sort' => 3,
            'is_system' => 1,
            'is_active' => 1,
        ],
        [
            'user_id' => null,
            'name' => '金融理财',
            'icon' => '💰',
            'color' => '#fa8c16',
            'description' => '银行、支付、投资理财',
            'sort' => 4,
            'is_system' => 1,
            'is_active' => 1,
        ],
        [
            'user_id' => null,
            'name' => '娱乐游戏',
            'icon' => '🎮',
            'color' => '#eb2f96',
            'description' => '游戏、视频、音乐娱乐',
            'sort' => 5,
            'is_system' => 1,
            'is_active' => 1,
        ],
        [
            'user_id' => null,
            'name' => '工作学习',
            'icon' => '📚',
            'color' => '#13c2c2',
            'description' => '办公、学习、教育平台',
            'sort' => 6,
            'is_system' => 1,
            'is_active' => 1,
        ],
    ];

    foreach ($systemCategories as $category) {
        $category['created_at'] = date('Y-m-d H:i:s');
        $category['updated_at'] = date('Y-m-d H:i:s');

        // 检查是否已存在
        $exists = Category::where('name', $category['name'])
            ->where('is_system', 1)
            ->find();

        if (!$exists) {
            Category::create($category);
            echo "已插入分类: {$category['name']}\n";
        } else {
            echo "分类已存在: {$category['name']}\n";
        }
    }

    echo "系统分类初始化完成！\n";

} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}